﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServer.DBAccess;
using WebServer.Models;

namespace WebServer.Biz
{
    public class NewsBiz
    {
        #region 分类逻辑
        /// <summary>
        /// 获取所有的子分类 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsCate> ManCate(bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = new NewsDA().ManCate(IsDel);
            return request;
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsCate> Cate(int ID, bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = new NewsDA().Cate(ID, IsDel);
            return request;
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public NewsCate CateByID(int ID)
        {
            NewsCate request = new NewsCate();
            request = new NewsDA().CateByID(ID);
            return request;
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public bool CateCreate(string Name, int ParentID)
        {
            bool request = false;
            request = new NewsDA().CateCreate(Name, ParentID);
            return request;
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool CateSet(int ID, string Name, int Parent, bool IsDel)
        {
            bool request = false;
            request = new NewsDA().CateSet(ID, Name, Parent, IsDel);
            return request;
        }

        /// <summary>
        /// 新闻动作
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Fild"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public static int CateAction(int ID, string Fild, bool Fn)
        {
            int result = 0;
            result = NewsDA.CateAction(ID, Fild, Fn);
            return result;
        }

        /// <summary>
        /// 获取判断是否有子节点的分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static List<NewsCate> CateList(int ID, bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = NewsDA.CateList(ID, IsDel);
            return request;
        }
        #endregion

        #region 资讯处理
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> MNewsList(int CateID, int PageSize, int PageIndex, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsDA().MNewsList(CateID, PageSize, PageIndex, Title, BourseName, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }


        /// <summary>
        /// 后台获取新闻总数
        /// </summary>
        /// <param name="CateID"></param>     
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>    
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public int MNewsCount(int CateID, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int IsDel)
        {
            int result = 0;
            result = new NewsDA().MNewsCount(CateID, Title, BourseName, IsStick, IsHeadline, HasImg,IsDel);
            return result;
        }

        /// <summary>
        /// 获取存在交易所
        /// </summary>
        /// <returns></returns>
        public List<NewsBourseGroupEntity> BourseGroup()
        {
            List<NewsBourseGroupEntity> request = new List<NewsBourseGroupEntity>();
            request = new NewsDA().BourseGroup();
            request = request.Where(o => string.IsNullOrEmpty(o.Name) == false && string.IsNullOrEmpty(o.Shortened) == false).ToList();
            for (int i = 0; i < request.Count(); i++)
            {
                if (request[i].Name.Contains("白盘"))
                {
                    request[i].Name = request[i].Name.Replace("白盘", "文交所");
                    request[i].Shortened = request[i].Shortened.Replace("白", "");
                }
                if (request[i].Name.Contains("晚盘"))
                {
                    request.Remove(request[i]);
                }
            }
            request = request.Where((x, i) => request.FindIndex(z => z.Name == x.Name) == i).ToList();
            return request;
        }


        /// <summary>
        /// 获取存在交易所【有centerID】
        /// </summary>
        /// <returns></returns>
        public List<NewsBourseGroupEntity> PBourseGroup()
        {
            List<NewsBourseGroupEntity> request = new List<NewsBourseGroupEntity>();
            request = new NewsDA().PBourseGroup();
            request = request.Where(o => string.IsNullOrEmpty(o.Name) == false && string.IsNullOrEmpty(o.Shortened) == false).ToList();
            //for (int i = 0; i < request.Count(); i++)
            //{
            //    if (request[i].Name.Contains("白盘"))
            //    {
            //        request[i].Name = request[i].Name.Replace("白盘", "文交所");
            //        request[i].Shortened = request[i].Shortened.Replace("白", "");
            //    }
            //    if (request[i].Name.Contains("晚盘"))
            //    {
            //        request.Remove(request[i]);
            //    }
            //}
            //request = request.Where((x, i) => request.FindIndex(z => z.Name == x.Name) == i).ToList();
            return request;
        }


        /// <summary>
        /// 根据交易所获取分类的存在分组
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public List<NewsCate> CateGroup(int Parent, string Name)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = new NewsDA().CateGroup(Parent,Name);
            return request;
        }

        /// <summary>
        /// 获取新闻详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public News NewsByID(int ID)
        {
            News request = new News();
            request = new NewsDA().NewsByID(ID);
            return request;
        }

        /// <summary>
        /// 新闻排行获取-不包含评论
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Size"></param>
        /// <param name="Sort">0:点击量;1:ID;;2创建时间;3:更新时间 ;4:评论</param>
        /// <param name="Field"></param>
        /// <returns></returns>
        public List<News> NewsBySort(int CateID, bool IsHeadle, int Size, int Sort, bool Desc)
        {
            List<News> request = new List<News>();
            request = new NewsDA().NewsBySort(CateID, IsHeadle, Size, Sort, Desc);
            return request;
        }



        /// <summary>
        /// 根据CenterID获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="CenterID"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> NewsListByCenterID(int CateID, int PageSize, int PageIndex, string Title, int CenterID, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsDA().NewsListByCenterID(CateID, PageSize, PageIndex, Title, CenterID, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsHEadLine"></param>
        /// <param name="HasImg"></param>
        /// <returns></returns>
        public List<News> NewsList(int CateID, int Page, int PageSize, string BourseName, bool IsStick, bool IsHeadline, bool HasImg, bool IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsDA().NewsList(CateID, Page, PageSize, BourseName,IsStick, IsHeadline, HasImg, IsDel);
            return request;
        }
        /// <summary>
        /// 获取没有解读的新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<News> NoReadingNewsList(int CateID, int Page, int PageSize, string BourseName, bool IsStick, bool IsHeadline, bool HasImg, bool IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsDA().NoReadingNewsList(CateID, Page, PageSize, BourseName, IsStick, IsHeadline, HasImg, IsDel);
            return request;
        }



        /// <summary>
        /// 新闻动作
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool NewsAction(int ID, string Filed, bool Fn)
        {
            bool request = false;
            request = new NewsDA().NewsAction(ID, Filed, Fn);
            return request;
        }

        /// <summary>
        /// 新增以及更新新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool NewsSet(News N)
        {
            bool request = false;
            N.Title = N.Title == "" ? "" : Common.SpecialKey(N.Title);
            N.SmallTitle = N.SmallTitle == "" ? "" : Common.SpecialKey(N.SmallTitle);
            N.Text = N.Text == "" ? "" : Common.SpecialKey(N.Text);
            N.Title = N.Title == "" ? "" : N.Title.Replace('\'', '‘');
            N.SmallTitle = N.SmallTitle == "" ? "" : N.SmallTitle.Replace('\'', '‘');
            N.Text = N.Text == "" ? "" : N.Text.Replace('\'', '‘');

            request = new NewsDA().NewsSet(N);
            return request;
        }


        /// <summary>
        /// 新增以及更新新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int NewsInfoSet(News N)
        {
            int result = 0;
            N.Description = N.Description == "" ? "" : Common.SpecialKey(N.Description);
            N.EditTitle = N.EditTitle == "" ? "" : Common.SpecialKey(N.EditTitle);
            N.TWord = N.TWord == "" ? "" : Common.SpecialKey(N.TWord);

            N.Title = N.Title == "" ? "" : N.Title.Replace('\'', '‘');
            N.SmallTitle = N.SmallTitle == "" ? "" : N.SmallTitle.Replace('\'', '‘');
            N.Text = N.Text == "" ? "" : N.Text.Replace('\'', '‘');

            result = new NewsDA().NewsInfoSet(N);

            NewsConKeys Entity = new NewsConKeys();

            Entity.NewsID = result;
            Entity.IsDel = false;

            N.Label = N.Label == null ? "" : N.Label;

            if (result > 0 && N.Label != "")
            {
                //此新闻的标签全部去除

                int DelLabelNews = new NewsDA().NewsConKeyDel(0, result, 0, false);

                string Remark = N.Label.TrimEnd(',');
                string[] OpenInfo = Remark.Split(',');
                try
                {
                    foreach (string item in OpenInfo)
                    {
                        Entity.Label = item == "" ? 0 : Convert.ToInt32(item);
                        int reques = new NewsDA().NewsConKeySet(Entity);
                    }
                }
                catch (Exception)
                {
                    
                    result = 0;
                }
            }
            else
            {
               
            }
            return result;
        }

        /// <summary>
        /// 获取新闻详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public News NewsInfoByID(int ID)
        {
            News request = new News();
            request = new NewsDA().NewsInfoByID(ID);

            return request;
        }


        /// <summary>
        /// 获取上一篇，下一篇
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<NewsKeys> NewsCon(int ID)
        {
            List<NewsKeys> request = new List<NewsKeys>();
            request = new NewsDA().NewsCon(ID);
            return request;
        }
        #endregion

        #region 标签
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsKeys> NewsLabel(int PageSize, int PageIndex, int ID, string Name, bool IsDel)
        {
            List<NewsKeys> request = new List<NewsKeys>();
            request = new NewsDA().NewsLabel(PageSize, PageIndex, ID, Name, IsDel);
            return request;
        }

        /// <summary>
        /// 标签总数
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Name"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public int NewsLabelCount(int ID, string Name, bool IsDel)
        {
            int result = 0;
            result = new NewsDA().NewsLabelCount(ID, Name, IsDel);
            return result;

        }

        /// <summary>
        /// 根据ID获取标签
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public NewsKeys NewsLabelByID(int ID)
        {
            NewsKeys entity = new NewsKeys();
            entity = new NewsDA().NewsLabelByID(ID);
            return entity;
        }


        /// <summary>
        /// 添加或者修改标签
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public MsgEntity NewsLabelSet(NewsKeys N)
        {
            MsgEntity M = new MsgEntity();

            int result = new NewsDA().NewsLabelSet(N);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else if (result == -1)
            {
                M.Code = 30000;
                M.Msg = "异常";
            }
            else
            {
                M.Code = 20000;
                M.Msg = "错误";
            }
            return M;
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Del">真删，还是假删</param>
        /// <returns></returns>
        public int NewsLabelDel(int ID, bool Del)
        {
            int result = 0;
            result = new NewsDA().NewsLabelDel(ID, Del);
            return result;
        }


        /// <summary>
        /// 新闻关联标签
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public int NewsConKeySet(NewsConKeys N)
        {

            int result = 0;
            result = new NewsDA().NewsConKeySet(N);
            return result;
        }
        #endregion

        #region 首页/咨询幻灯片
        /// <summary>
        /// 幻灯片
        /// </summary>
        /// <returns></returns>
        public static List<Slide> getImg(int IsOpen, int AppOrWeb)
        {
            List<Slide> result = new List<Slide>();
            result = NewsDA.getImg(IsOpen, AppOrWeb);
            return result;
        }


        /// <summary>
        /// 后台查询所有幻灯片
        /// </summary>
        /// <returns></returns>
        public static List<Slide> getMImg(int IsOpen, int AppOrWeb, int Order, bool Desc)
        {
            List<Slide> result = new List<Slide>();
            result = NewsDA.getMImg(IsOpen, AppOrWeb, Order, Desc);
            return result;
        }


        /// <summary>
        /// 删除幻灯片
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int DelImg(int ID)
        {
            int result = 0;
            result = NewsDA.DelImg(ID);
            return result;
        }

        /// <summary>
        /// 新增或者修改幻灯片
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddOrUpImg(Slide model)
        {
            int result = 0;
            model.Title = model.Title == "" ? "" : Common.SpecialKey(model.Title);
            model.Name = model.Name == "" ? "" : Common.SpecialKey(model.Name);
            model.Text = model.Text == "" ? "" : Common.SpecialKey(model.Text);

            result = NewsDA.AddOrUpImg(model);
            return result;
        }

        /// <summary>
        /// 根据ID获取幻灯片
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static Slide ImgByID(int ID)
        {
            Slide entity = new Slide();
            entity = NewsDA.ImgByID(ID);
            return entity;
        }
        #endregion

        #region 解读
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> ReadingList(int CateID, int PageSize, int PageIndex, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel, int UserID)
        {
            List<News> request = new List<News>();
            request = new NewsDA().ReadingList(CateID, PageSize, PageIndex, Title, BourseName, IsStick, IsHeadline, HasImg, Order, Desc, IsDel, UserID);
            return request;
        }


        /// <summary>
        /// 手机查询解读
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> PReadingList(int CateID, int PageSize, string OpenTime, string EndTime, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel, int UserID)
        {
            List<News> request = new List<News>();
            request = new NewsDA().PReadingList(CateID, PageSize, OpenTime, EndTime, Title, BourseName, IsStick, IsHeadline, HasImg, Order, Desc, IsDel, UserID);
            return request;
        }


        /// <summary>
        /// 总数
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="IsDel"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int ReadingCount(int CateID, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int IsDel, int UserID)
        {
            int result = 0;
            result = new NewsDA().ReadingCount(CateID,  Title, BourseName, IsStick, IsHeadline, HasImg,  IsDel, UserID);
            return result;
        }


        /// <summary>
        /// 经济商发表解读
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public bool ReadingSet(News N)
        {

            bool request = false;
            N.Title = N.Title == "" ? "" : Common.SpecialKey(N.Title);
            N.SmallTitle = N.SmallTitle == "" ? "" : Common.SpecialKey(N.SmallTitle);
            N.Text = N.Text == "" ? "" : Common.SpecialKey(N.Text);

            request = new NewsDA().ReadingSet(N);
            return request;

        }

        /// <summary>
        /// 解读删除
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity ReadingDel(int ID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            int back = new NewsDA().ReadingDel(ID, UserID);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "执行成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "执行失败";
            }
            return request;
        }


        /// <summary>
        /// 解读删除
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public int DelReading(int ID, string Admin)
        {
            int result = 0;
            result = new NewsDA().DelReading(ID, Admin);
            return result;
        }
        #endregion

        #region 手机资讯
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> PNewsList(string OpenTime, string EndTime, int PageSize, int CateID, string Title, string Name, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsDA().PNewsList(OpenTime, EndTime, PageSize, CateID, Title, Name, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }
        #endregion
    }
}
